from db import  db_handler
from lib import common

user_logger = common.get_logger('shop')

# 查看用户接口
def check_user_interface(user):
    # 调用数据处理层的select函数，查看用户信息
    user_dic = db_handler.select(user)

    if user_dic:
        return True

import hashlib
# 注册接口
def register_interface(user,pwd,balance=15000):
    pwd = common.get_md5(pwd)
    user_dic = {
        'user': user,
        'pwd': pwd,
        'balance': balance,
        'flow':[],
        'shop_cart':{},
        'lock':False,
        'admin':False,
    }

    # 调用保存数据功能
    db_handler.save(user_dic)
    user_logger.info(f'{user_dic["user"]}用户注册成功！')
    return f'{user_dic["user"]}用户注册成功！'
uregister_interface_logger = common.get_logger('register_interface')
# 登陆接口
def login_interface(user,pwd):
    user_dic = db_handler.select(user)
    pwd = common.get_md5(pwd)
    if user_dic['pwd'] == pwd:
        return True, f'{user}登陆成功'
    return False,f'{user}密码错误'

# 查看余额
def check_bal_interface(user):
    user_dir = db_handler.select(user)
    return user_dir['balance']

def logout_interface():
    from core import src
    user = src.user_info['user']
    src.user_info['user'] = None
    return True,f'{user}用户注销成功，欢迎下次登录'

def check_user_lock(user):
    user_dir = db_handler.select(user)
    return user_dir['lock']

def check_admin_lock(user):
    user_dir = db_handler.select(user)
    return user_dir['admin']

def be_admin_interface(user):
    user_dic = db_handler.select(user)

    user_dic['admin'] = True

    db_handler.save(user_dic)
    return f'{user}注册管理员成功'

